package com.lyn.pluginbazaar.utils;

import com.lyn.pluginbazaar.dao.BaseRepository;
import com.lyn.pluginbazaar.dao.plugin.PluginDao;
import com.lyn.pluginbazaar.entity.PagingQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Map;

public class JpaUtil {

    public static <R extends BaseRepository<T>, T> boolean save(R r, T t) {
        T save = r.save(t);
        if (save == null) {
            return false;
        }
        return true;
    }

    public static <R extends BaseRepository<T>, T> boolean delete(PluginDao r, Class<T> t, long id) {
        try {
            r.deleteById(id);
            return true;
        } catch (Throwable ignored) {
        }
        return false;
    }

    public static <R extends BaseRepository<T>, T> boolean update(R r, T t) {
        T saveAndFlush = r.saveAndFlush(t);
        if (saveAndFlush == null) {
            return false;
        }
        return true;
    }

    public static <R extends BaseRepository<T>, T> List<T> pagingQuery(R r, Class<T> t, PagingQuery pagingQuery) {
        Page<T> all = r.findAll(new ISpecification<T>(pagingQuery), new IPageable(pagingQuery));
        if (!all.isEmpty()) {
            return all.toList();
        }
        return null;
    }

    public static <R extends BaseRepository<T>, T> long count(R r, Class<T> t) {
        return r.count();
    }

    public static <R extends BaseRepository<T>, T> long count(R r, Class<T> t,PagingQuery pagingQuery) {
        return r.count(new ISpecification<>(pagingQuery));
    }


    static class ISpecification<T> implements Specification<T> {

        private final PagingQuery pagingQuery;

        public ISpecification(PagingQuery pagingQuery) {
            this.pagingQuery = pagingQuery;
        }

        @Override
        public Specification<T> and(Specification<T> other) {
            return null;
        }

        @Override
        public Specification<T> or(Specification<T> other) {
            return null;
        }

        @Override
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
            Predicate predicate = cb.conjunction();
            if (pagingQuery.getEndTime() != 0) {
                predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("time"), pagingQuery.getStartTime()));
                predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("time"), pagingQuery.getEndTime()));
            }
            Map<String, Object> parameter = pagingQuery.getParameter();
            if (parameter != null) {
                for (String key : parameter.keySet()) {
                    if (parameter.get(key) instanceof Boolean) {
                        continue;
                    }
                    Object values = parameter.get(key);
                    if (!StringUtils.isEmpty(values)) {
                        predicate.getExpressions().add(cb.equal(root.get(key), parameter.get(key)));
                    }
                }
            }

            Map<String, Object> likeParameter = pagingQuery.getLikeParameter();
            if (likeParameter != null) {
                for (String key : likeParameter.keySet()) {
                    if (likeParameter.get(key) instanceof Boolean) {
                        continue;
                    }
                    Object values = likeParameter.get(key);
                    if (!StringUtils.isEmpty(values)) {
                        predicate.getExpressions().add(cb.like(root.get(key).as(String.class), "%" + likeParameter.get(key) + "%"));
                    }
                }
            }
            return predicate;
        }
    }


    static class IPageable implements Pageable {

        private final PagingQuery pagingQuery;

        public IPageable(PagingQuery pagingQuery) {
            this.pagingQuery = pagingQuery;
        }

        @Override
        public int getPageNumber() {
            return pagingQuery.getPage();
        }

        @Override
        public int getPageSize() {
            return pagingQuery.getSize();
        }

        @Override
        public long getOffset() {
            return pagingQuery.getPage() * pagingQuery.getSize() - pagingQuery.getSize();
        }

        @Override
        public Sort getSort() {
            return Sort.by(Sort.Direction.DESC, "time");
        }

        @Override
        public Pageable next() {
            return null;
        }

        @Override
        public Pageable previousOrFirst() {
            return null;
        }

        @Override
        public Pageable first() {
            return null;
        }

        @Override
        public Pageable withPage(int pageNumber) {
            return null;
        }

        @Override
        public boolean hasPrevious() {
            return false;
        }
    }

}
